﻿
Partial Class WebForm_ManageRoleUserMappingByUser_OpenManageUserRoleMapping
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub
    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim RoleID As String = TGridView1.DataKeys(DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex).Value.ToString()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        If DirectCast(sender, CheckBox).Checked = True Then
            sql.AppendFormat(" Insert Into RoleUserMapping Values(  ")
            sql.AppendFormat(" '{0}', ", RoleID)
            sql.AppendFormat(" '{0}') ", Me.EmployeeID)

            Try
                'MyObj.ShowMessage("人員角色設定成功");
                dat.AccData(sql)
            Catch
                MyObj.ShowMessage("人員角色設定失敗")
            End Try
        Else
            sql.AppendFormat(" Delete From RoleUserMapping ")
            sql.AppendFormat(" Where RoleID = '{0}' ", RoleID)
            sql.AppendFormat(" And UserID = '{0}' ", Me.EmployeeID)

            Try
                'MyObj.ShowMessage("人員角色設定成功");
                dat.AccData(sql)
            Catch
                MyObj.ShowMessage("人員角色設定失敗")
            End Try
        End If

        Me.mpSearch.Show()

    End Sub

    Protected Sub TGridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TGridView1.PageIndexChanged
        Me.mpSearch.Show()

    End Sub

    Protected Sub TGridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles TGridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If DirectCast(e.Row.FindControl("lblMyRoleID"), Label).Text <> "" Then
                DirectCast(e.Row.FindControl("CheckBox1"), CheckBox).Checked = True
            End If
        End If
    End Sub
    Public Sub InitDataAndShow()
        Me.mpSearch.Show()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" select A.*,B.RoleID as MyRoleID,B.UserID as MyUserID From ")
        sql.AppendFormat(" (Select * from Role) A ")
        sql.AppendFormat(" Left join ")
        sql.AppendFormat(" (select * from RoleUserMapping Where UserID='{0}') B ", Me.EmployeeID)
        sql.AppendFormat(" On A.RoleID = B.RoleID ")

        TGridView1.PageIndex = 0

        TGridView1.DataSource = dat.GetData(sql)
        TGridView1.DataBind()
    End Sub
    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "1", True, False))
    End Function

    Public Property EmployeeID() As String
        Get
            Return ViewState("EmployeeID")
        End Get
        Set(ByVal value As String)
            ViewState("EmployeeID") = value
        End Set
    End Property



End Class
